<?php

tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');
/**
 * 后台部门管理
 * 
 * @author jason
 */
class CollectAction extends AdministratorAction
{
	public $pageTitle = array(
							'index'  => '采集用户',
							'getUserRecord' =>'数据信息',
							);
	
	
	public function _initialize(){
		//$this->pageTitle['index'] = L('PUBLIC_DEPARTMENT_SETTING');
		$this->pageTitle['index'] = '采集用户';
        $this->pageTitle['newslist'] = '数据信息';
		$this->pageTab[] = array('title' => '采集用户' , 'tabHash' => 'index' , 'url' => U('admin/Collect/index'));
        $this->pageTab[] = array('title' => '数据信息' , 'tabHash' => 'newslist' , 'url' => U('admin/Collect/getUserRecord'));
		parent::_initialize();
	}
	public function index(){
		$this->pageKeyList = array('id','name','url','lastupdatetime','oprate');
		$this->pageButton[] = array('uid','title'=>'添加', 'onclick'=>"location.href='".U('admin/Collect/addUser')."';");

		/*数据的格式化 与listKey保持一致 */
		$map = array();
		//!empty($_POST['name']) && $map['b.name'] = array('like', "%" . t($_POST['name']) . "%");
		//!empty($_POST['table']) && $map['_string'] = "`table` = '".t($_POST['table'])."'";

 		$listData = D('CollectUser')->getCollectUserList($map);
		//var_dump($listData);die;
 		
 		foreach($listData['data'] as &$v){
 			$v['oprate'] = '<a href="javascript:;" onclick="admin.collectdel(this,'.$v['id'].')">'.L('删除').'</a>';
			$v['oprate'] .= ' | <a href="javascript:;" onclick="admin.collectall(this,'.$v['id'].')">'.L('采集全部').'</a>';
			$v['oprate'] .= ' | <a href="javascript:;" onclick="admin.onekeypublish(this,'.$v['id'].')">'.L('一键发布').'</a>';
			$v['lastupdatetime'] = date('Y-m-d H:i:s',$v['lastupdatetime']);
 		}

 		$this->allSelected = false;
		$this->displayList($listData);
		
	}
	
	public function addUser(){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$_POST['lastupdatetime'] = time();
			$cu = D('CollectUser');
			$cu->create();
			$s = $cu->add();
			//var_dump($s,$_POST);die;
			if($s){
				$this->assign('jumpUrl',U('admin/Collect/index'));
                return $this->success( '添加成功');
			}
			else{
				$this->assign('jumpUrl',U('admin/Collect/index'));
                return $this->success( '添加失败');
			}
		}
		$this->savePostUrl = U('admin/Collect/addUser');
		$this->pageKeyList = array('name','url');		
		$this->displayConfig();
	}
	
	public function getUserRecord(){		
		$m = M('collect_record');
		$listData = $m->findPage(20);
		$this->pageKeyList = array('id','cuid','vurl','addtime','oprate');
		foreach($listData['data'] as &$v){
 			$v['oprate'] = '<a href="javascript:;" onclick="admin.collectdel(this,'.$v['id'].')">'.L('删除').'</a>';			
			$v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
 		}
		$this->allSelected = false;
		$this->displayList($listData);
	}
	
	public function collectdel(){
		$id = intval($_POST['id']);
		if($id < 1){
			die(json_encode(array('status'=>0,'data'=>'非法参数')));
		}
		$cu = D('CollectUser');
		$s = $cu->delUser($id);
		if($s){
			die(json_encode(array('status'=>1,'data'=>'删除成功')));
		}
		else{
			die(json_encode(array('status'=>0,'data'=>'删除失败')));
		}
		
	}
	
	public function collectall(){		
		$id = intval($_POST['id']);
		if($id < 1){
			die(json_encode(array('status'=>0,'data'=>'非法参数')));
		}
		$getOneUserInfo = D('CollectUser')->getOneUser($id);
		if(empty($getOneUserInfo)){
			die(json_encode(array('status'=>0,'data'=>'获取用户信息失败')));
		}
		tsload(ADDON_PATH.'/library/Youku.class.php');
		$yk = new Youku();
		$param = C('YOUKU');
		$param['url'] = $getOneUserInfo['url'];
		$param['id'] = $id;
		//$param['addtime'] = time();
		$yk->_init($param);
		$pageNum = $yk->getPageNum();
		if($pageNum <= 0){
			die(json_encode(array('status'=>0,'data'=>'页数未获取到')));
		}
		//$info = array();
		$cr = M('collect_record');
		$s = 0;$w = 0;
		for($i=$pageNum;$i>=1;$i--){
			$param['url'] =  substr($param['url'],0,-1) . $i;
			$yk->_init($param);
			$data =  $yk->getUrls();
			foreach($data as $v){
				$flag = $cr->add($v);
				if($flag){
					$s++;//成功数
				}
				else{
					$w++;//失败数
				}
			}
		}
		//file_put_contents('data.txt',var_export($info,true));
		die(json_encode(array('status'=>1,'data'=>"成功{$s}条，失败{$w}条")));
		
		
	}
	//一键发布
	public function oneKeyPublish(){
		ini_set("max_execution_time", 0); 
		$id = intval($_POST['id']);
		if($id < 1){
			die(json_encode(array('status'=>0,'data'=>'非法参数')));
		}
		$cr = M('collect_record');
		$data = $cr->where(array('cuid'=>$id))->select();
		$s = 0;$w = 0;
		foreach($data as $v){			
			if(preg_match("/(youku.com|youtube.com|qq.com|ku6.com|sohu.com|sina.com.cn|tudou.com|yinyuetai.com)/i", $v['vurl'], $hosts)) {	  		
				$d['videourl'] = $v['vurl'];
			}		
			$flashinfo = model('Video')->_video_getflashinfo($v['vurl'], $hosts[1]);
			
			if( !$flashinfo['title'] || json_encode($flashinfo['title']) == 'null'){
				continue;
			}
			$d['body'] = $flashinfo['title'].$d['videourl'];
			$type = 'postvideo';
			$app = 'public';		
			$flag = model('Feed')->put($this->uid, $app, $type, $d);
			D('Channel', 'channel')->setChannel($data['feed_id'], 1, false);//频道id需要手动设置
			if($flag){
				$s++;//成功数
			}
			else{
				$w++;//失败数
			}
		}
		
		die(json_encode(array('status'=>1,'data'=>"成功{$s}条，失败{$w}条")));
		
	  	//exit( json_encode($return) );
		
		
	}
	
}